package com.story.modular.app.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.story.common.Util.ExcelExport2;
import com.story.common.constant.factory.PageFactory;
import com.story.common.exception.BizExceptionEnum;
import com.story.common.exception.BussinessException;
import com.story.common.persistence.model.PayDetail;
import com.story.core.base.controller.BaseController;
import com.story.core.shiro.ShiroKit;
import com.story.modular.app.dao.PayDetailMapper;
import com.story.modular.app.service.PayDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Pyx
 * @PACKAGE_NAME : com.story.modular.app.controller
 * @FileName: PayDetailController
 * @date 2020/4/27 10:07
 */
@Controller
@RequestMapping("payDetail")
@Api(value = "第二批 支付记录", tags = "第二批 支付记录")
public class PayDetailController extends BaseController {
    @Autowired
    private PayDetailMapper payDetailMapper;
    @Autowired
    private PayDetailService payDetailService;
    private static String PREFIX = "/system/payDetail/";

    @RequestMapping("index")
    public String index() {
        return PREFIX + "payDetail.html";
    }

    @RequestMapping("/payDetail_add")
    public String payDetailAdd() {
        return PREFIX + "payDetail_add.html";
    }

    @ApiOperation("得到列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Object list(String startTimeS, String endTimeS) throws ParseException {
        if (startTimeS == null || Strings.isNullOrEmpty(startTimeS)) {
            startTimeS = "2019-01-01 00:00:00";
        }
        if (endTimeS == null || Strings.isNullOrEmpty(endTimeS)) {
            endTimeS = "2050-01-01 00:00:00";
        }
        Page<PayDetail> page = new PageFactory<PayDetail>().defaultPage();
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTimeForm = format.parse(startTimeS);
        Date endTimeForm = format.parse(endTimeS);
        return payDetailMapper.findAll(page, startTimeForm, endTimeForm);
    }

    @ApiOperation("导出excel")
    @RequestMapping(value="/ExcelDownload",method = RequestMethod.POST)
    public void excelDownload(HttpServletResponse response, String startTimeS, String endTimeS) throws IOException {

        String[] arr = new String[]{"id", "用户名", "支付类型", "备注", "支付金额",
                "发生日期"
                , "会员开始日期", "会员结束日期","余额"};
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTimeForm = null, endTimeForm = null;
        if (startTimeS == null || Strings.isNullOrEmpty(startTimeS)) {
            startTimeS = "2019-01-01 00:00:00";
        }
        if (endTimeS == null || Strings.isNullOrEmpty(endTimeS)) {
            endTimeS = "2050-01-01 00:00:00";
        }
        try {
            startTimeForm = format.parse(startTimeS);
            endTimeForm = format.parse(endTimeS);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        ExcelExport2.export(response,
                payDetailMapper.export(startTimeForm, endTimeForm)
                , arr);
    }

    @ApiOperation("创建订单 payType 1为支付宝 2 微信 vipType 会员类型")
    @PostMapping("/create")
    public ResponseEntity create(Integer payType, Integer vipType) {
        PayDetail payDetail = new PayDetail();
        if (payType.equals(1)) {
            payDetail.setPayType("支付宝");
        } else if (payType.equals(2)) {
            payDetail.setPayType("微信");
        } else {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }

        payDetail.setVipType(vipType);
        int save = payDetailService.save(payDetail,null);
        if (save > 0) {
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("msg","success");
            return ResponseEntity.ok(map);
        } else {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
    }

    @ApiOperation("登陆以后判断是否是会员  1是普通会员 2vip会员")
    @PostMapping("/findByUserIsVIP")
    public ResponseEntity findByUser() {
        Integer userId = ShiroKit.getUser().getId();
        List<PayDetail> details = payDetailMapper.findByUser(userId);
        Map<String,Object> map = new HashMap<String,Object>();
        if (details.size() == 0) {
            map.put("msg","普通会员");
            map.put("code","200");
            map.put("data","1");
        } else {
            PayDetail payDetail = details.get(0);
            long stampTime = payDetail.getEndTime().getTime() - System.currentTimeMillis();
            if(stampTime >0){
                map.put("msg","vip会员");
                map.put("code","200");
                map.put("data","2");
            }else{
                map.put("msg","普通会员");
                map.put("code","200");
                map.put("data","1");
            }
        }
        return ResponseEntity.ok(map);
    }

}
